#!/usr/bin/env bash

container=$1
echo "container: $container"

#
# print usage
#
function printUsage() {
    echo "usage: bash $0 tomcat|jetty|undertow"
    echo "  e.g: bash $0 tomcat"
    echo ""
}

#
# setup
#
function setup() {
    echo "setup..."
    mv pom.xml pom.xml.tmp
    echo "setup done."
}

#
# set container pom
#
function setContainerPom() {
    echo "set container pom..."
    cp pom_$container.xml pom.xml
    echo "set container pom done."
}

#
# build app
#
function buildApp() {
    mvn clean package -Dmaven.test.skip=true
}

#
# clean
#
function clean() {
    echo "clean..."
    mv pom.xml.tmp pom.xml
    echo "clean done."
}

if [[ ! "$container" ]]; then
    printUsage
    exit 1
fi

if [[ "tomcat" != "$container" ]] && [[ "jetty" != "$container" ]] && [[ "undertow" != "$container" ]]; then
    echo "Unknown container"
    printUsage
    exit 1
fi

setup
setContainerPom $container
buildApp
clean
